package com.zhengtoudai.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;
import com.zhengtoudai.R;
import com.zhengtoudai.base.App;
import com.zhengtoudai.base.MvpFragment;
import com.zhengtoudai.global.Config;
import com.zhengtoudai.http.HttpUtil;
import com.zhengtoudai.injector.component.DaggerActivityComponent;
import com.zhengtoudai.injector.module.ActivityModule;
import com.zhengtoudai.otto.LoginOutEvent;
import com.zhengtoudai.ui.activity.AccountSettingActivity;
import com.zhengtoudai.ui.activity.DelegatioNominisActivity;
import com.zhengtoudai.ui.activity.FundRecordActivity;
import com.zhengtoudai.ui.activity.LoginActivity;
import com.zhengtoudai.ui.activity.MainActivity;
import com.zhengtoudai.ui.activity.MessageCenterActivity;
import com.zhengtoudai.ui.activity.MyInvestmentActivity;
import com.zhengtoudai.ui.activity.MyRedpacketActivity;
import com.zhengtoudai.ui.activity.MyShareActivity;
import com.zhengtoudai.ui.activity.RealNameCertificationActivity;
import com.zhengtoudai.ui.activity.RechargeActivity;
import com.zhengtoudai.ui.activity.WebViewActivity;
import com.zhengtoudai.ui.activity.WithDrawActivity;
import com.zhengtoudai.ui.bean.MyFragmentModel;
import com.zhengtoudai.ui.presenter.MyFragmentPresenter;
import com.zhengtoudai.ui.presenter.ShengTenpayPresenterImpl;
import com.zhengtoudai.ui.view.MyFragmentView;
import com.zhengtoudai.ui.view.ShengTenpeyView;
import com.zhengtoudai.util.AbToastUtil;
import com.zhengtoudai.util.GlideHelper;
import com.zhengtoudai.util.Tool;
import com.zhengtoudai.view.swipetoloadlayout.base.OnRefreshListener;
import com.zhengtoudai.view.swipetoloadlayout.base.SwipeToLoadLayout;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.zhengtoudai.R.id.refresh;

/**
 * 作者：黑哥 on 2016/7/29 11:21
 */
public class MyFragment extends MvpFragment<MyFragmentPresenter> implements MyFragmentView, OnRefreshListener, ShengTenpeyView {
    @Bind(R.id.iv_head_portrait)
    ImageView mIvHeadPortrait;
    @Bind(R.id.tv_accumulated_income)
    TextView mTvAccumulatedIncome;
    @Bind(R.id.tv_account_balance)
    TextView mTvAccountBalance;
    @Bind(R.id.tv_total_assets_value)
    TextView mTvTotalAssetsValue;
    @Bind(R.id.tv_account_balance_value)
    TextView mTvAccountBalanceValue;
    @Bind(R.id.ll_recharge)
    LinearLayout mLlRecharge;
    @Bind(R.id.ll_withdraw)
    LinearLayout mLlWithdraw;
    @Bind(R.id.tv_setting)
    TextView mTvSetting;
    @Bind(R.id.tv_investment)
    TextView mTvInvestment;
    @Bind(R.id.tv_transfer)
    TextView mTvTransfer;
    @Bind(R.id.tv_transaction)
    TextView mTvTransaction;
    @Bind(R.id.tv_my_redpacket)
    TextView mTvMyRedpacket;
    @Bind(R.id.tv_share)
    TextView mTvShare;
    @Bind(R.id.tv_opinion)
    TextView mTvOpinion;
    @Bind(R.id.swipe_target)
    ScrollView mSwipeTarget;
    @Bind(R.id.tv_name)
    TextView mTvName;
    @Bind(refresh)
    SwipeToLoadLayout mRefresh;
    private MainActivity activity;
    private View view;

    @Inject
    ShengTenpayPresenterImpl mPresenter;

    @Inject
    Bus mBus;

    private MyFragmentModel.ResultEntity mInfo;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my, container, false);
        activity = (MainActivity) getActivity();

        super.onCreateView(inflater, container, savedInstanceState);

        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    protected void initTitle() {
        titleUtil.setTitle("我的", false);
    }

    @Override
    protected void initView() {
        DaggerActivityComponent.builder().appComponent(App.mApp.getAppComponent())
                .activityModule(new ActivityModule(getActivity()))
                .build()
                .inject(this);
        mBus.register(this);
        mPresenter.attachView(this);
        mRefresh.setOnRefreshListener(this);
        mRefresh.setLoadMoreEnabled(false);

    }


    @Override
    protected void setData() {
        mvpPresenter .loadData(mContext);

    }


    @Override
    protected MyFragmentPresenter createPresenter() {
        return new MyFragmentPresenter(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (App.mApp.hasLogin()) {
            mvpPresenter.loadData(mContext);
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mBus.unregister(this);
    }

    @Subscribe
    public void LoginOutUpdateIU(LoginOutEvent event) {
        mTvName.setText("未登录");
        mTvAccumulatedIncome.setText("0.00");
        mTvAccountBalanceValue.setText("0.00");
        mTvTotalAssetsValue.setText("0.00");
        titleUtil.setTitle("我的",false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.detachView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.ll_recharge, R.id.ll_withdraw, R.id.tv_setting, R.id.tv_investment, R.id.tv_transfer, R.id.tv_transaction, R.id.tv_my_redpacket, R.id.tv_share, R.id.tv_opinion})
    public void onClick(View view) {
        if (Tool.isFastDoubleClick()) return;
        if (!App.mApp.hasLogin()) {
            startActivity(LoginActivity.class);
            AbToastUtil.showToast(mContext, "请先登录");
            return;
        }
        switch (view.getId()) {
            case R.id.ll_recharge:
                if (mInfo!=null){
                    if (mInfo.getIsDeposit().equals("1")){
                        startActivity(RechargeActivity.class);
                    }else{
                        startActivity(RealNameCertificationActivity.class);
                        AbToastUtil.showToast(mContext,"请先绑定盛付通");
                    }
                }else {
                    AbToastUtil.showToast(mContext,"正在获取用户信息...");
                    mvpPresenter.loadData(mContext);
                }

                break;
            case R.id.ll_withdraw:
                if (mInfo!=null){
                    if (mInfo.getIsDeposit().equals("1")){
                        startActivity(WithDrawActivity.class);
                    }else{
                        startActivity(RealNameCertificationActivity.class);
                        AbToastUtil.showToast(mContext,"请先绑定盛付通");
                    }
                }else {
                    AbToastUtil.showToast(mContext,"正在获取用户信息...");
                    mvpPresenter.loadData(mContext);
                }
                break;
            case R.id.tv_setting:
                startActivity(AccountSettingActivity.class);
                break;
            case R.id.tv_investment:
                startActivity(MyInvestmentActivity.class);
                break;
            case R.id.tv_transfer:
                startActivity(DelegatioNominisActivity.class);
                break;
            case R.id.tv_transaction:
                startActivity(FundRecordActivity.class);
                break;
            case R.id.tv_my_redpacket:
                startActivity(MyRedpacketActivity.class);
                break;
            case R.id.tv_share:
                startActivity(MyShareActivity.class);
                break;
            case R.id.tv_opinion:
                startActivity(MessageCenterActivity.class);
                break;
        }
    }

    //得到数据
    @Override
    public void getDataSuccess(MyFragmentModel model) {
        this.mInfo=model.getResult();
        MyFragmentModel.ResultEntity mInfo = model.getResult();
        Log.e("LW", "得到返回数据==" + mInfo.toString());
        GlideHelper.showCircleImage(activity, Config.BASE_URL + mInfo.getAvatarImg(), R.mipmap.my_head, mIvHeadPortrait);
        mTvName.setText(mInfo.getUserAccount());
        //总资产
        mTvTotalAssetsValue.setText(mInfo.getAllMoney());
        //累计收益
        mTvAccumulatedIncome.setText(mInfo.getSumGet());
        //账户余额
        mTvAccountBalanceValue.setText(mInfo.getAvailableMoney());

        setSFTStatus(mInfo.getIsDeposit());

    }

    /**
     * 跳转盛付通
     *
     * @param isDeposit 0为未注册  则跳转注册盛付通。1为注册了 跳转到登录
     */
    private void setSFTStatus(final String isDeposit) {

        String title;
        if (isDeposit.equals("1")) {
            title = "查看盛付通";
        } else {
            title = "绑定盛付通";
        }
        titleUtil.setTitleOrRight(false,"我的", title, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isDeposit.equals("1")) {
                    String url = Config.BASE_URL + "appAccountCorrelation/tgLogin.app";
                    mPresenter.getShengTenpay(url);
                } else {
                    startActivity(RealNameCertificationActivity.class);
                }
            }
        });

    }

    //得到错误信息
    @Override
    public void getDataFail(String msg) {

    }

    @Override
    public void hideLoading() {
        mRefresh.setRefreshing(false);
    }

    @Override
    public void showLoading() {

    }


    @Override
    public void onRefresh() {
        if (App.mApp.hasLogin()){
            mvpPresenter.loadData(mContext);
        }else{
            startActivity(LoginActivity.class);
            AbToastUtil.showToast(mContext,"请先登录");
            mRefresh.setRefreshing(false);
        }
    }

    @Override
    public void showDialog() {
        showProgressDialog("正在跳转中...");
    }

    @Override
    public void onFailure(Throwable e) {
        HttpUtil.disposeOnFailure(mContext, e);
    }

    @Override
    public void onResult(String html) {
        Bundle bundle = new Bundle();
        bundle.putString("title", "盛付通");
        bundle.putString("html", html);
        startActivity(bundle, WebViewActivity.class);
    }

    @Override
    public void dismissDialog() {
        dismissProgressDialog();
    }
}
